package com.java.servlet.web.commons;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

@WebServlet (name = "SalirController", urlPatterns="/salir/salirController")
public class SalirController extends HttpServlet {
	private static final long serialVersionUID = -2614171834488786806L;
	private static final Logger log = Logger.getLogger(SalirController.class);
	private static final int LOGOUT_USER = 0;
	private static final int SESSION_USER_EXISTE = 1;
	private Properties viewConfig;

	public SalirController(){
	}
    public void init() {
		try {
			super.init();
			viewConfig = PropertiesUtils.getPropertiesFromFile(getServletContext().getResource("/WEB-INF/view-config.properties"));
		} catch (MalformedURLException ex) {
			log.info("Error fatal, no se puedo cargar el archivo de configuracion de vistas." + ex);
		} catch(ServletException serEx){
			log.info("Error fatal, no se puedo cargar el archivo de configuracion de vistas." + serEx);
		}
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
		int metodo = Integer.parseInt(request.getParameter("method"));
		RequestDispatcher dispatcher = null;
		try {
			switch (metodo) {
			case LOGOUT_USER:
				HttpSession sessionApp = request.getSession(false);
				if(sessionApp != null){
					sessionApp.invalidate();
				}
				dispatcher = request.getRequestDispatcher(viewConfig.getProperty("init.login.user"));
				dispatcher.forward(request, response);
				break;
			case SESSION_USER_EXISTE:
				System.out.println("session.no.existe------------------------");
				dispatcher = request.getRequestDispatcher(viewConfig.getProperty("session.no.existe"));
				dispatcher.forward(request, response);
				break;
			}
		} catch (Exception e) {
            request.setAttribute("ex", e);
            dispatcher = request.getRequestDispatcher(viewConfig.getProperty("error"));
            dispatcher.forward(request, response);
			System.out.println("Error en LoginController " + e);
			e.printStackTrace();
		} finally {
            out.close();
        }
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
}
